#include "basic.h"

//==============================================================================
static struct termios ori_attr, cur_attr;
void set_fl(int fd, int flags)
{
    int     val;
    if ( (val = fcntl(fd, F_GETFL, 0)) < 0)
        perror("fcntli get");
   
    val |= flags;
   
    if (fcntl(fd, F_SETFL, val) < 0)
        perror("fcntl set");
}
void clr_fl(int fd, int flags)
{
    int     val;
    if ( (val = fcntl(fd, F_GETFL, 0)) < 0)
        perror("fcntl get");
   
    val &= ~flags;
   
    if (fcntl(fd, F_SETFL, val) < 0)
        perror("fcntl set");
}
int kbhit(void)
{
                  
        fd_set rfds;
        struct timeval tv;
        int retval;

        /* Watch stdin (fd 0) to see when it has input. */
        FD_ZERO(&rfds);
        FD_SET(0, &rfds);
        /* Wait up to five seconds. */
        tv.tv_sec  = 0;
        tv.tv_usec = 0;

        retval = select(1, &rfds, NULL, NULL, &tv);
        /* Don't rely on the value of tv now! */

        if (retval == -1) {
                perror("select()");
                return 0;
        } else if (retval)
                return 1;
        /* FD_ISSET(0, &rfds) will be true. */
        else
                return 0;
        return 0;
}
int tty_set(void)
{
        
        if ( tcgetattr(STDIN_FILENO, &ori_attr) )
                return -1;
        
        memcpy(&cur_attr, &ori_attr, sizeof(cur_attr) );
        cur_attr.c_lflag &= ~ICANON;
//        cur_attr.c_lflag |= ECHO;
        cur_attr.c_lflag &= ~ECHO;
        cur_attr.c_cc[VMIN] = 1;
        cur_attr.c_cc[VTIME] = 0;

        if (tcsetattr(STDIN_FILENO, TCSANOW, &cur_attr) != 0)
                return -1;

        return 0;
}
int tty_reset(void)
{
        if (tcsetattr(STDIN_FILENO, TCSANOW, &ori_attr) != 0)
                return -1;

        return 0;
}

//==============================================================================
int find_libs( char *lib_dir, char *module_dir, char **libs, int libs_size )
{
	assert( lib_dir && module_dir && libs && libs_size);
	
	char _dir[512] = "\0";
	strcpy( _dir, lib_dir );
	strcat( _dir, module_dir);
	DIR *d = opendir( _dir );
	if( 0 == d ) return -1;
	
	struct dirent *di = NULL;
	int i=0, l=0;
	
	for( i=0 ; i<libs_size ; i++ ){
		if( (di = readdir(d)) != NULL ){
			if( di->d_type == DT_REG || di->d_type == DT_LNK ){
				char *lib = (char *)malloc( strlen(module_dir) + strlen(di->d_name) + 5 );
				strcpy( lib, module_dir );
				strcat( lib, "/" );
				strcat( lib, di->d_name );
				libs[l] = lib;
				l++;
			}
		}else break;
	}

	closedir( d );
	libs[l] = NULL;
	return l;
}

//==============================================================================
unsigned int copy_pointer( int **dest, int **src, int mode)
{	
	int *temp = NULL;
	int **_dest = dest;
	int **_src = src;
	unsigned int count = 0;
	
	if( mode < 0 ){
		do{
			temp = *_src;
			*_src++ = *_dest;
			*_dest++ = temp;
			count++;
		}while( ++mode );
		
	}else if( mode > 0 ){
		do{
			temp = *_src++;
			*_dest++ = temp;
			count++;
		}while( --mode );
		
	}else{
		do{
			*_dest++ = *_src++;
			count++;
		}while(*_src);
		*_dest = NULL;
	}
	
	return count;
}


